<template>
    <el-table :data="adminList" style="width: 100%" v-loading="loading">
        <el-table-column prop="adminid" label="管理员id" />
        <el-table-column prop="adminname" label="管理员名字" width="180" />
        <el-table-column prop="role" label="角色" width="180">
            <template #default="scope">
                <span>{{ scope.row.role == 1 ? "超级管理员" : "普通管理员" }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" v-if="crud.charAt(3)==='1'">
            <template #default="scope">
                <el-popconfirm confirm-button-text="是的" cancel-button-text="否" title="您确定要删除吗?" hide-icon="true"
                @confirm="del(scope.row.adminid)">
                    <template #reference>
                        <el-button type="danger">删除</el-button>
                    </template>
                </el-popconfirm>
                <el-button type="success"  @click="revise(scope.row.adminname)" v-if="crud.charAt(2)==='1'">修改</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-dialog show-close="false"     v-model="flag"
    width="10%"
    align-center>
    <eject v-show="flag" @successs="fly" :adminname="adm" @transmit="closes"></eject>
</el-dialog>
</template>
<script setup lang="ts">
import service from "../../../until/service";
import { ref,reactive } from "vue"
import { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css';
import eject from "./eject.vue"
import {useRoute} from "vue-router";
import {useRouteStore} from "../../../piniaStore/index"
const routeStore = useRouteStore();
const loading = ref(routeStore.loading)
const $route = useRoute();
const crud:string = ($route.meta.crud as string);
interface Human {
    adminid: string,
    password: number,
    role: number,       //1超级 2普通
    adminname: string,
    "checkedKeys": string[]
}

const adminList = reactive<Human[]>([]);
// 获取管理员列表
const sumAll = () => {
    service({
        url: '/admin/list'
    }).then(res => {
        if (res.data.code === '200') {
            adminList.length = 0
            adminList.push(...res.data.data)
        }
    })
}
sumAll()
// 需要传的管理名
const adm = ref('')
// let quan = ref<any>([])
const flag = ref(false)
// 显示修改框
const revise = (names:string)=>{
    adm.value = names
    // quan.value = arr
    flag.value = true
}
// 关闭修改框
const closes =()=>{
    flag.value = false

}
// 修改后重新发获取列表的请求
const fly=()=>{
    sumAll()
}
const del =(val:string)=>{
    service({
        url:'/admin/delete',
        method:'post',
        data:{
            adminid:val
        }
    }).then(res=>{
        if(res.data.code === '200'){
            adminList.find((element, item) => {
        if (element.adminid == val) {
          adminList.splice(item, 1)
        }
      });
            ElMessage({
                message:"删除成功",
                type:"success"
            })
        }
    })
}
</script>
<style scoped>
</style>